using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame3
{
    /// <summa>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        int[,] map = new int[10,10];
        Drawable[,] texmap = new Drawable[10, 10];
        Texture2D[] sample = new Texture2D[5];
        Texture2D tmp;
        Character link;
        KeyboardState touche = new KeyboardState();
        int i = 0;
        string strmap = File.ReadAllText("C:\\Users\\Sharr\\Documents\\Visual Studio 2010\\Projects\\my-zelda-like\\WindowsGame3\\WindowsGame3\\WindowsGame3Content\\map.txt");

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            graphics.PreferredBackBufferWidth = 390;
            graphics.PreferredBackBufferHeight = 370;
            graphics.ApplyChanges();
            base.Initialize();
            
        }

        protected void initmat()
        {
            int j = 0;
            int k = 0;
            char a;
            for (int i = 0; i < strmap.Length;i++ )
            {
                if ((strmap[i] != '\n') && (strmap[i] != '\r'))
                {
                    a = strmap[i];
                    map[j, k] = Convert.ToInt16(a) - 48;
                    k++;
                }
                else if (strmap[i] == '\r')
                {
                    k = 0;
                    j++;
                }
            }
        }
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            link = new Character(0, 0, Content.Load<Texture2D>("marche"), 100, null, null);
            initmat();
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            sample[0] = Content.Load<Texture2D>("sol");
            sample[1] = Content.Load<Texture2D>("arbre");
            sample[2] = Content.Load<Texture2D>("trou");
            sample[3] = Content.Load<Texture2D>("eau");
            sample[4] = Content.Load<Texture2D>("eauprof");


            for (int i = 0; i < 10;i++ )
            {
                for (int j = 0; j < 10;j++ )
                {
                    if (map[i,j] == 0)
                    {
                        texmap[i, j] = new Drawable(i * sample[0].Width, j * sample[0].Height, sample[0]);
                    }
                    else if (map[i, j] == 1)
                    {
                        tmp = Content.Load<Texture2D>("arbre");
                        texmap[i, j] = new Drawable(i * sample[1].Width, j * sample[1].Height, sample[1]);
                    }
                    else if (map[i, j] == 2)
                    {
                        tmp = Content.Load<Texture2D>("trou");
                        texmap[i, j] = new Drawable(i * sample[2].Width, j * sample[2].Height, sample[2]);
                    }
                    else if (map[i, j] == 3)
                    {
                        tmp = Content.Load<Texture2D>("eau");
                        texmap[i, j] = new Drawable(i * sample[3].Width, j * sample[3].Height, sample[3]);
                    }
                    else if (map[i, j] == 4)
                    {
                        tmp = Content.Load<Texture2D>("eauprof");
                        texmap[i, j] = new Drawable(i * sample[4].Width, j * sample[4].Height, sample[4]);
                    }
                    else if (map[i, j] == 5)
                    {
                        tmp = Content.Load<Texture2D>("sol");
                        texmap[i, j] = new Drawable(i * sample[0].Width, j * tmp.Height, tmp);
                    }
                    
                }
            }
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            
            // TODO: Add your update logic here
            touche = Keyboard.GetState();
            if (i == 0)
            {
                deplace();
                i = 0;
            }
            else
            {
                i++;
            }
            base.Update(gameTime);
        }
        protected void deplace()
        {
            if (touche.IsKeyDown(Keys.W))
            {
                if (Colision.coli(link,texmap,0,-1))
                {
                link.move(0, -1);
                link.Update();
                }
            }
            else if (touche.IsKeyDown(Keys.S))
            {
                if (Colision.coli(link, texmap, 0, 1))
                {
                    link.move(0, 1);
                    link.Update();
                }
            }
            else if (touche.IsKeyDown(Keys.D))
            {
                if (Colision.coli(link, texmap, 1, 0))
                {
                    link.move(1, 0);
                    link.Update();
                }
            }
            else if (touche.IsKeyDown(Keys.A))
            {
                if (Colision.coli(link, texmap, -1, 0))
                {
                    link.move(-1, 0);
                    link.Update();
                }
            }
            else if (touche.IsKeyDown(Keys.O))
            {
                //utiliser l'objet droit
            }
            else if (touche.IsKeyDown(Keys.P))
            {
                //utiliser l'objet gauche
            }
            else if (touche.IsKeyDown(Keys.Enter))
            {
                //start
            }
            else if (touche.IsKeyDown(Keys.Space))
            {
                //select
            }
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            
            GraphicsDevice.Clear(Color.White);
            spriteBatch.Begin();
            for (int i = 0; i<10;i++)
            {
                for (int j = 0;j<10;j++)
                {
                    spriteBatch.Draw(texmap[j,i].tex,texmap[i,j].pos,Color.White);
                }
            }
            spriteBatch.End();
            link.Draw(spriteBatch, link.pos);
            
            // TODO: Add your drawing code here
           
            base.Draw(gameTime);
        }
    }
}
